﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;
using CNotebook.Inventory;

namespace NotebookS2
{
    public partial class M_BRND_CT : RibbonForm
    {
        public M_BRND_CT()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }

        void BindBrand(string _ItemGroup)
        {
            BrandManager oManager = new BrandManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable _table = oManager.GetBrands(_ItemGroup);
            oManager.Close();

            cboBrand.DisplayMember = "BrandName";
            cboBrand.ValueMember = "BrandID";
            cboBrand.DataSource = _table;
            cboBrand.Refresh();



        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text == "Add")
            {
                BrandCategoryManager oBrndCT = new BrandCategoryManager();

                oBrndCT.ConnectionString = this.ConnectionString;
                oBrndCT.Open();

                if (oBrndCT.IsCategoryExists(txtCategoryCode.Text) == false)
                {
                    oBrndCT.AddBrandCategory(cboBrand.SelectedValue.ToString(), txtCategoryCode.Text, txtCategoryName.Text);
                    txtCategoryCode.Properties.ReadOnly = true;

                    MessageBox.Show("Category already added!!!", "Catgry", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    btnAdd.Text = "Update";
                }
                else
                {
                    MessageBox.Show("Category already exists!!!", "Catgry", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                oBrndCT.Close();
            }
            else
            {
                BrandCategoryManager oBrndCT = new BrandCategoryManager();

                oBrndCT.ConnectionString = this.ConnectionString;
                oBrndCT.Open();

                    oBrndCT.UpdateBrandCategory(cboBrand.SelectedValue.ToString(), txtCategoryCode.Text, txtCategoryName.Text);

                    MessageBox.Show("Category already updated!!!", "Catgry", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    btnAdd.Text = "Update";
                
                oBrndCT.Close();
            }
        }

        private void M_BRND_CT_Load(object sender, EventArgs e)
        {
            BindBrand(cboItemGroup.Text);
        }

        private void cboItemGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {

                BindBrand(cboItemGroup.Text);
                
            }
            catch
            {
            }
        }

        private void txtCategoryCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FND_BRND_CT oFindBrandCT = new FND_BRND_CT();

            oFindBrandCT.ConnectionString = this.ConnectionString;
            oFindBrandCT.BrndID = cboBrand.SelectedValue.ToString();
            oFindBrandCT.BrndName = cboBrand.Text;
            oFindBrandCT.ShowDialog();

            string sCatgryCode = oFindBrandCT.CategoryCode;

            if (sCatgryCode != null)
            {
                BrandCategoryManager oManager = new BrandCategoryManager();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();
                DataTable _table = oManager.GetBrandCategory(sCatgryCode);
                oManager.Close();

                DataRow oRow = _table.Rows[0];

                txtCategoryCode.Text = sCatgryCode;
                txtCategoryName.Text = oRow["CatgryName"].ToString();
                txtCategoryCode.Properties.ReadOnly = true;
                btnAdd.Text = "Update";
            }

        }

        private void cboBrand_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtCategoryCode.Text = "";
            txtCategoryName.Text = "";
            txtCategoryCode.Properties.ReadOnly = false;
            btnAdd.Text = "Add";
        }
    }
}
